package me.riverslob.test.esper.plugin.esperio.rpc.input;

import com.espertech.esper.client.EventBean;
import com.espertech.esper.core.service.EPServiceProviderSPI;
import org.springframework.context.ApplicationContext;

import java.util.Map;

/**
 * <br/>Created on 13-10-14 上午11:21
 *
 * @author 郑治明（zm.zheng@zhuche.com）
 * @since 3.2.2
 */
public class EsperIORPCService implements EsperIORPCRequestHandler {
//    private static final Logger log = LoggerFactory.getLogger(EsperIORPCService.class);

    private final EPServiceProviderSPI engineSPI;
//    private final Map<String, EsperHttpRequestCacheEntry> streamCache = new HashMap<String, EsperHttpRequestCacheEntry>();
//    private final MethodResolutionServiceImpl methods;

    public EsperIORPCService(EPServiceProviderSPI engine) {
        this.engineSPI = engine;
//        this.methods = new MethodResolutionServiceImpl(engineSPI.getEngineImportService(), engineSPI.getTimeProvider());
    }

    public void start() {
        ApplicationContext appContext = null;
        EsperIORPCRemoteService remoteService= (EsperIORPCRemoteService) appContext.getBean("");
        remoteService.setHandler(this);
    }

    public void destroy(){

    }


    @Override
    public void handle(Map<String, Object> dataMap) {
//        String eventTypeName = (String) dataMap.get("stream");
        EventBean theEvent = (EventBean) dataMap.get("data");

        /*EsperHttpRequestCacheEntry cacheEntry = streamCache.get(eventTypeName);
        if (cacheEntry == null) {
            cacheEntry = makeCacheEntry(eventTypeName);
            streamCache.put(eventTypeName, cacheEntry);
        }

        if (cacheEntry == null) {
            return;
        }

        Object[] values = new Object[cacheEntry.getParsers().length];
        for (int i = 0; i < cacheEntry.getParsers().length; i++) {
            String value = dataMap.get(cacheEntry.getWritableProperties()[i].getPropertyName());
            if (value == null) {
                continue;
            }
            values[i] = cacheEntry.getParsers()[i].parse(value);
        }

        EventBean theEvent = cacheEntry.getEventBeanManufacturer().make(values);
        */
        engineSPI.getEPRuntime().sendEvent(theEvent);

    }

/*
    private EsperHttpRequestCacheEntry makeCacheEntry(String eventTypeName) {

        EventType eventType = engineSPI.getEventAdapterService().getExistsTypeByName(eventTypeName);
        if (eventType == null) {
            log.info("Event type by name '" + eventTypeName + "' not found.");
            return null;
        }

        if (!(eventType instanceof EventTypeSPI)) {
            log.info("Event type by name '" + eventTypeName + "' is not writable.");
            return null;
        }

        EventTypeSPI eventTypeSPI = (EventTypeSPI) eventType;

        Set<WriteablePropertyDescriptor> writablesSet = engineSPI.getEventAdapterService().getWriteableProperties(eventTypeSPI, false);
        List<WriteablePropertyDescriptor> writablePropertiesList = new ArrayList<WriteablePropertyDescriptor>();
        List<SimpleTypeParser> parserList = new ArrayList<SimpleTypeParser>();

        for (WriteablePropertyDescriptor writableDesc : writablesSet)
        {
            SimpleTypeParser parser = SimpleTypeParserFactory.getParser(writableDesc.getType());
            if (parser == null) {
                log.debug("No parser found for type '" + writableDesc.getType() + "'");
                continue;
            }

            writablePropertiesList.add(writableDesc);
            parserList.add(parser);
        }

        WriteablePropertyDescriptor[] writableProperties = writablePropertiesList.toArray(new WriteablePropertyDescriptor[writablePropertiesList.size()]);
        SimpleTypeParser[] parsers = parserList.toArray(new SimpleTypeParser[parserList.size()]);

        EventBeanManufacturer eventBeanManufacturer;
        try {
            eventBeanManufacturer = engineSPI.getEventAdapterService().getManufacturer(eventType, writableProperties, methods.getEngineImportService(), false);
        }
        catch (EventBeanManufactureException e) {
            log.info("Unable to create manufacturer for event type: " + e.getMessage(), e);
            return null;
        }

        return new EsperHttpRequestCacheEntry(eventBeanManufacturer, writableProperties, parsers);
    }
*/
}
